/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 07 - 18
 * Time: 15:54
 */
//力扣606. 根据二叉树创建字符串
public class Solution6 {
    public String tree2str(TreeNode root) {
       StringBuilder stringBuilder = new StringBuilder();
       tree2strChild(root, stringBuilder);
       return stringBuilder.toString();
    }
    private void tree2strChild(TreeNode root, StringBuilder stringBuilder) {
        if(root == null) {//这个条件要加
            return;
        }
        stringBuilder.append(root.val);
        if(root.left != null) {
            stringBuilder.append("(");
            tree2strChild(root.left, stringBuilder);
            stringBuilder.append(")");
        }else {
            if(root.right == null) {
                return;
            }else {
                stringBuilder.append("()");
            }
        }
        if(root.right != null) {
            stringBuilder.append("(");
            tree2strChild(root.right, stringBuilder);
            stringBuilder.append(")");
        }else {
            return;
        }
    }
}
